package iotest.byteio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFileDemo {
	public static void main(String[] args) {
		long time1 = System.currentTimeMillis();
		copyFile();
		long time2 = System.currentTimeMillis();
		copyFile1();
		long time3 = System.currentTimeMillis();
		System.out.println(time2-time1);
		System.out.println(time3-time2);
	}

	private static void copyFile1() {
		InputStream inputStream = null;
		OutputStream outputStream = null;
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			inputStream = new FileInputStream("src/byteio/OutputStreamDemo.java");
			bufferedInputStream = new BufferedInputStream(inputStream);
			outputStream = new FileOutputStream("out.java");
			bufferedOutputStream = new BufferedOutputStream(outputStream);
			
			byte[] bs = new byte[1024];
			int len = -1;
			while ((len = bufferedInputStream.read(bs)) != -1) {
				bufferedOutputStream.write(bs, 0, len);
			}
			bufferedOutputStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (bufferedOutputStream != null) {
					bufferedOutputStream.close();
				}
				if (bufferedInputStream != null) {
					bufferedInputStream.close();
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private static void copyFile() {
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			bufferedInputStream = new BufferedInputStream( new FileInputStream("src/byteio/OutputStreamDemo.java"));
			bufferedOutputStream =new BufferedOutputStream( new FileOutputStream("out1.java"));
			int len = -1;
			while ((len = bufferedInputStream.read()) != -1) {
				bufferedOutputStream.write(len);
			}
			bufferedOutputStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (bufferedOutputStream != null) {
					bufferedOutputStream.close();
				}
				if (bufferedInputStream != null) {
					bufferedInputStream.close();
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
